/**
 * 管理员
 * */


//列表
function getList(){
    var formData = {
        menu_id: menu_id,
        start: pages.start, limit: pages.limit,
    };
    $.ajax({
        type: apis.yewuIndex.method,
        url: apis.yewuIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                console.log(res); return
            }
            $('#pageTotal').html(res.total);
            listHtml(res.data);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//列表字符串
function listHtml(d){
    var h = '', i, ftype, fval;
    for (i = 0; i < d.length; i ++) {
        ftype = d[i].ftype;
        fval = d[i].val;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].key + '</td>';
        h += '<td width="50%">';
        if (ftype == 3) {
            h += '<img src="' + fval + '" width="50%"/>';
        } else {
            h += fval;
        }
        h += '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="list_edit(\'修改\',\'yewu-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
        h += '修改<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<input type="hidden" id="val' + d[i].id + '" value="' + d[i].val + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//获取一个
function getOne(v){
    $.ajax({
        type: apis.yewuOne.method,
        url: apis.yewuOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var ftype = res.data.ftype;
            var val = res.data.val;
            var h = '', i;
            if (ftype == 3) {
                h = '';
                h += '<div class="row cl">';
                h += '<label class="form-label col-xs-4 col-sm-3">图片</label>';
                h += '<div class="formControls col-xs-8 col-sm-8">';
                h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">上传图片</div>';
                h += '<input type="file" id="upload" style="width: 150px; height: 30px; opacity: 0; cursor: pointer; position: relative; top: -25px; left: -20px;" onchange="uploadImage();"/>';
                h += '<div style="margin-top: -20px; width: 100%; height: 300px; background: #f9f9f9; overflow: auto;"><img src="" id="viewimg"/><input type="hidden" id="img"/></div>';
                h += '</div>';
                h += '</div>';
            } else if (ftype == 4) {
                h = '';
                h += '<div class="row cl">';
                h += '<label class="form-label col-xs-4 col-sm-3">内容</label>';
                h += '<div class="formControls col-xs-8 col-sm-8">';
                h += '<textarea class="textarea" style="height: 200px;" id="val">' + val + '</textarea>';
                h += '<div style="font-size: 12px; color: #888;">注意：字段之间用||||隔开，行之间回车行号换行。</div>';
                h += '</div>';
                h += '</div>';
            } else {
                h = '';
                h += '<div class="row cl">';
                h += '<label class="form-label col-xs-4 col-sm-3">内容</label>';
                h += '<div class="formControls col-xs-8 col-sm-8">';
                h += '<input type="text" class="input-text" id="val" value="' + val + '"/>';
                h += '</div>';
                h += '</div>';
            }
            $('#ftype')[0].value = ftype;
            $('#form').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function list_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,500);
}


//保存更新
function modify(){
    var ftype = $('#ftype').val();
    var formData = {
        id: id, val: ''
    };
    if (ftype == '') {
        msgPop({msg: '没有类型'}); return
    }
    if (ftype == 3) {
        formData.val = $('#img').val();
    } else {
        formData.val = $('#val').val();
    }
    if (formData.val == '') {
        msgPop({msg: '字段值必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.yewuModify.method,
        url: apis.yewuModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '修改成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}